package interview;

public class lc977 {
    public static void main(String[] args) {
        int[] res = sortedSquares(new int[]{-5,-3,-2,-1});
        for (int i : res) {
            System.out.print(i + " ");
        }
    }
    public static int[] sortedSquares(int[] nums) {
        int n = nums.length;
        int[] result = new int[n];
        int index = n - 1;
        int left = 0, right = n - 1;
        while(left <= right){
            if(nums[left] * nums[left] > nums[right] * nums[right]){
                result[index--] = nums[left] * nums[left];
                left++;
            }
            else{
                result[index--] = nums[right] * nums[right];
                right--;
            }
        }
        return result;
    }
}
